package com.model;

import dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * Created by caicf on 2016/6/12.
 */
public class TestUser {
    public static void main(String[] args) throws IOException {

        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

//1.select    ==================================================================================
        List<User> users1=userMapper.getUsers();


        for (User user : users1) {
            System.out.println(user.getId()+" " +user.getName()+" "+user.getDesc1());

        }

////2.insert        =====================================================================================
////
//        User u = new User();
//        u.setName("caicf1");
//        u.setDescription("loging qiongxuesheng ");
//
//        User u1 = new User();
//        u1.setName("fxd1");
//        u1.setDescription("wife");
//
//        User u2 = new User();
//        u2.setName("marry1");
//        u2.setDescription("jiehun");
//
//        List<User> users = new ArrayList<User>();
//        users.add(u);
//        users.add(u1);
//        users.add(u2);
//
//        int i = userMapper.insertUser(users);
//        System.out.println(i);
//        System.out.println(u.getId());
//        System.out.println(u1.getId());
//        System.out.println(u2.getId());
//3、Update
//        User newUser=new User();
//        newUser.setId(660);
//        newUser.setName("kaoshi");
//        int updateR=userMapper.updateUser(newUser);
//        System.out.println(updateR);
// 4、一对一联合查询   查询用户与角色
//        List<User> usersRole=userMapper.selectUserRole();
//        for (User user : usersRole) {
//            System.out.println(user.getId()+"\t"+user.getName()+"\t"+user.getDesc1()+"\t"+user.getRole().getName());
//        }
//5、查询拥有某个角色的所有用户


    }

}
